package com.doit.day03


object _04_平均温度案例 {
  def main(args: Array[String]): Unit = {
    val d1 = Array(("beijing", 28.1), ("shanghai", 28.7), ("guangzhou", 32.0), ("shenzhen", 31.0))
    val d2 = Array(("beijing", 27.3), ("shanghai", 30.1), ("guangzhou", 33.3))
    val d3 = Array(("beijing", 28.2), ("shanghai", 29.1), ("guangzhou", 32.0), ("shenzhen", 32.0))

    //求这三天，每一个城市的平均温度

    //1.先将三个数组都放在一个数组中
    val allCityAndTem: Array[(String, Double)] = d1 ++ d2 ++ d3

    //2.根据城市分组
    // beijing ，Array(("beijing", 28.1)，("beijing", 27.3)，("beijing", 28.2))
    val grouped : Map[String,Array[(String, Double)]] = allCityAndTem.groupBy((tp: (String, Double)) => {
      tp._1
    })

    //3.用map映射获取后面的温度，然后来求平均值
    val res: Map[String, Double] = grouped.map((tp: (String, Array[(String, Double)])) => {
      val city: String = tp._1
      val tuple_city_tep: Array[(String, Double)] = tp._2
      val teps: Array[Double] = tuple_city_tep.map((tp1: (String, Double)) => tp1._2)
      val avg_tem: Double = (teps.sum / teps.size).formatted("%.2f").toDouble
      (city, avg_tem)
    })

    res.foreach(println)

    println("===========")

    //装杯的写法
    allCityAndTem.groupBy((tp:(String,Double))=>{tp._1})
    allCityAndTem.groupBy((tp:(String,Double))=>tp._1)
    allCityAndTem.groupBy(tp =>tp._1)
    allCityAndTem.groupBy(_._1)


    allCityAndTem.groupBy(_._1)
      .map(tp=>{
        (tp._1,(tp._2.map(_._2).sum/tp._2.size).formatted("%.2f").toDouble)
      })
      .foreach(println)


    //函数的至简原则

    val arr: Array[Int] = Array(1, 2, 3)

    arr.map((a:Int)=>{a*10})
    arr.map((a:Int)=>a*10)
    arr.map( a =>a*10)
    arr.map( _ * 10)



  }

}
